﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
using System.IO;
using System.Drawing;

namespace FaceMessanger.Web
{
    public struct EnumUserStatus
    {
        public const int Online = 0;
        public const int Busy = 1;
        public const int OutDoors = 2;
        public const int Invisible = 3;
        public const int Offline = 4;
    }
    public class DUser
    {
        public Members Member;
        public List<Members> Friends;
    }

    public class ChatFriend
    {
        public Guid id { get; set; }
        public bool acikmi { get; set; }

    }

    /// <summary>
    /// Summary description for WebService1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class CarService : System.Web.Services.WebService
    {

        [WebMethod]
        public String GetUser()
        {
            DigitalAmeleEntities db = new DigitalAmeleEntities();
            DUser dUser = new DUser();
            dUser.Member = (from c in db.Members
                            where c.Id == new Guid("F2654457-980D-47FB-BEAB-5C50E5347640")
                            select c).First<Members>();
            var friends = (from c in db.Friends
                           where c.MyMemberId == new Guid("F2654457-980D-47FB-BEAB-5C50E5347640")
                           select c).ToList<Friends>();
            dUser.Friends = new List<Members>();
            foreach (Friends item in friends)
            {
                Members member = (from c in db.Members
                                  where c.Id == item.FriendMemberId
                                  select c).First<Members>();
                dUser.Friends.Add(member);
            }


            String sonuc = @"<table class='style1'>" +
                            "<tr class='style3'>" +
                            "<td class='style2'>" +
                            "</td>" +
                            "<td>" +
                            dUser.Member.FirstName + " " + dUser.Member.LastName +
                            "</td>" +
                            "</tr>";

            foreach (var item in dUser.Friends)
            {
                if (item.IsStatus != EnumUserStatus.Invisible)
                {
                    sonuc += @"<tr class='style3'>" +
                                "<td class='style2'>";
                    if (item.IsOnline == false)
                        sonuc += "Offline";
                    else
                        switch (item.IsStatus)
                        {
                            case EnumUserStatus.Online:
                                sonuc += "Online";
                                break;
                            case EnumUserStatus.Offline:
                                sonuc += "Offline";
                                break;
                            case EnumUserStatus.OutDoors:
                                sonuc += "OutDoors";
                                break;
                            case EnumUserStatus.Busy:
                                sonuc += "Busy";
                                break;
                            default:
                                break;
                        }
                    sonuc += @"</td>" +
                       "<td> <a id='" + item.Id.ToString() + "' class='mesajci'  href='#'>" +
                       item.FirstName + " " + item.LastName +
                       "</a> <input type='button' onclick='YeniMesajPanosuAc(\"" + item.Id.ToString() + "\")' id='Button2' class='mesajci' value='Get Cars' /> </td>" +
                       "</tr>";
                }
            }

            sonuc += "</table>";

            return sonuc;
        }

        [WebMethod(EnableSession = true)]
        public string YeniMesajPanosuAc(string friendid, string acikmi)
        {
            Boolean acik = false;
            Guid friendidg = new Guid(friendid);
            if (acikmi == "200")
            {
                acik = true;
            }

            List<ChatFriend> ChatFriendList = new List<ChatFriend>();
            if (Context.Session["ChatFriendList"] != null)
            {
                ChatFriendList = (List<ChatFriend>)Context.Session["ChatFriendList"];

            }
            bool var = false;
            ChatFriend cf = new ChatFriend();
            foreach (var item in ChatFriendList)
            {
                if (item.id == friendidg)
                {
                    item.acikmi = acik;
                    var = true;
                    break;
                }
            }

            if (!var)
            {
                ChatFriendList.Add(new ChatFriend() { id = friendidg, acikmi = false });
            }

            Context.Session["ChatFriendList"] = ChatFriendList;

            return MesajlariAl();
        }

        private string MesajlariAl()
        {
            Session.Add("UserId", "F2654457-980D-47FB-BEAB-5C50E5347640");
            Guid userid = new Guid(Session["UserId"].ToString());
            DigitalAmeleEntities db = new DigitalAmeleEntities();
            List<Guid> sonuc = (from d in db.InstantChat
                                where d.ReceiverMemberId == userid
                                select d.SenderMemberId).Distinct().ToList<Guid>();
            List<ChatFriend> ChatFriendList = new List<ChatFriend>();
        
            if (Context.Session["ChatFriendList"] != null)
            {
                ChatFriendList = (List<ChatFriend>)Context.Session["ChatFriendList"];
            }


            foreach (var item in sonuc)
            {
                bool var = false;
                foreach (var item1 in ChatFriendList)
                {
                    if (item1.id==item)
                    {
                        var = true;
                    }
                }
                if (!var)
                {
                    ChatFriendList.Add(new ChatFriend() { id = item, acikmi = true });
                }
            }

            List<String> ChatPanels = new List<string>();
            ChatPanels.Add(@"<table style='float: right'>
                                <tr>
                                   ");
            foreach (var item in ChatFriendList)
            {
                String chatpanel = "";

                Members friends = (from d in db.Members
                                   where d.Id == item.id
                                   select d).First<Members>();

                List<InstantChat> sonuc1 = (from d in db.InstantChat
                                            where ((d.SenderMemberId == item.id && d.ReceiverMemberId == userid) || (d.SenderMemberId == userid && d.ReceiverMemberId == item.id))
                                            orderby d.DateOfPost descending
                                            select d).ToList<InstantChat>();



                chatpanel += @" <td valign='bottom'>
                                         <div id='d" + item + @"' class='chatpanel'>
                                            <div class='subpanel'>
                                                <table style='width: 100%; height: 100%' border='0' cellpadding='0' cellspacing='0'>
                                                     <tr>
                                                        <td id='" + item + @"' onclick='MesajPanasunuAc('"+item+"')' class='h3'>";
                chatpanel += @"<span>&ndash; </span>" + friends.FirstName + " " + friends.LastName;
                chatpanel += @" </td>
                                </tr>
                                <tr>
                                    <td>
                                        <div style='height:150px; overflow: auto;'>
                                            <ul> ";


                foreach (var item1 in sonuc1)
                {
                    chatpanel += @" <li><a href='#'>
                                      <img src='" + item1.SenderMemberAvatarUrl + "' alt='' />";
                    chatpanel += item1.Message;
                    chatpanel += @"  </a></li>";
                }

                chatpanel += @" </ul>
                                        </div>
                                    </td>
                                </tr>";
                chatpanel += @" <tr style='height: 30px'>
                                    <td style='border-width: thin; height: 30px; border-color: inherit; border-top-style: groove;'>
                                        <input type='text' ID='TextBox2' BorderWidth='0' Height='30px' Style='vertical-align: middle'
                                            Width='100%'/>
                                    </td>
                                </tr>
                            </table>
                        </div>
                    </div>
                  </td>";

                ChatPanels.Add(chatpanel);
            }
            ChatPanels.Add(@" </tr></table>");
            string sonuc11 = "";
            foreach (var item in ChatPanels)
            {
                sonuc11 += item;
            }
            return sonuc11;
        }

    }
}
